package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"net/http"
	"test/utils/maptest"
	"test/utils/testerror"
	"time"
)

func init() {
	fmt.Println("init运行开始")

}

var upgrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool {
		return true // 允许所有来源
	},
}

func handleWebSocket(c *gin.Context) {
	conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to upgrade to WebSocket"})
		return
	}
	defer conn.Close()

	for {
		// 读取消息
		messageType, message, err := conn.ReadMessage()
		if err != nil {
			break
		}

		// 打印接收到的消息
		println(string(message))
		//获取当前时间
		Now := time.Now()
		// 发送消息
		err = conn.WriteMessage(messageType, []byte(Now.Format("2006-01-02 15:04:05")))
		if err != nil {
			break
		}
	}
}

// 1.注意首字母大写其他包才能使用
// 2.T代表类型V代表值
func init() {
	//controltest.Test()
	//utils.Array() //数组
	//slicetest.Test() //切片
	//fmt.Println("init2")
	//zhizheng.Zhizhen() //指针&获取
	maptest.Test() //man测试
	//structtest.Test() //结构体
	//controltest.Test() // 流程控制
	//functest.Test() //函数
	testerror.Error2()
	fmt.Printf("%T", "hello")

}
func main() {
	fmt.Println("运行结束")
	//r := gin.Default()
	//r.GET("/ws", handleWebSocket)
	//r.Run(":8080")
	/*	router := gin.Default()
		router.GET("/", func(c *gin.Context) {
			Now := time.Now()
			c.JSON(200, gin.H{
				"message":  Now.Format("2006-01-02 15:04:0"),
				"message2": 111,
			})
		})
		//加载静态文件
		router.LoadHTMLGlob("templates/*")
		router.GET("/hello", func(c *gin.Context) {
			c.HTML(200, "index.html", gin.H{
				"title": "hello world",
			})
		})
		router.Run()*/

}
